package Proc::Lock;

use JIP::ToolSet; # load my commonly-used modules in a single import
use parent qw( Class::Base Class::Accessor::Fast );

use IO::File;
use Carp qw( croak );
use English qw( -no_match_vars );

__PACKAGE__->mk_ro_accessors( qw( _lockFile ) );

sub init {
    my( $self, $config ) = @ARG;

    $self->{'_lockFile'} = $config->{'lock_file'}
        or croak('required parameter "lock_file" not defined');

    return $self;
}

sub lock {
    my $self = shift;

    my $outputHandler = IO::File->new(
        $self->_lockFile,
        O_WRONLY | O_CREAT | O_EXCL
    ) or croak( "can't open pid file: ". $OS_ERROR );

    $outputHandler->print( "<PID>". $PROCESS_ID ."</PID>" );

    $outputHandler->close;
}

sub unlock {
    my $self = shift;

    unlink( $self->_lockFile ) or croak( "can't unlink pid file: ". $OS_ERROR );
}

1;
